﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3
{
   
    public partial class NewJobCode : Form
        
    {
          private bool nonNumberEntered = false;
       
          private String connString = Program.ConnStr;
        public JobCodeDataBean jobcodedatabean = null;

        public NewJobCode()
        {
      
            InitializeComponent();
            newjobecodenoget();
            cmbcustomercode.Focus();
        }

        /// <summary>
        /// constructor to load when user select the customer from customer search
        /// </summary>
        /// <param name="CompanyName"></param>
        /// <param name="Customercode"></param>
        public NewJobCode(String CompanyName, String Customercode)
        {

            InitializeComponent();
            txtcompanyname.Text = CompanyName;
            cmbcustomercode.Text = Customercode;
            txtcompanyname.Enabled = false;
            newjobecodenoget();
            
            
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
        
       







        /// <summary>
        /// validating controlls for null
        /// return sucess if everything is ok
        /// </summary>
        /// <returns>sucess</returns>


        public Boolean validationcontrol()
        {
            Boolean sucess = false;
            if (txtjobcode.Text.Trim() == null || txtjobcode.Text.Trim() == "")
            {
                lblstatus.Text = "Enter Invoice";
                txtjobcode.Focus();
                txtjobcode.Text = "";
            }
            else if (cmbcustomercode.Text.Trim() == null || cmbcustomercode.Text.Trim() == "")
            {

                cmbcustomercode.Focus();
                lblstatus.Text = "Enter an Customer Code";
                cmbcustomercode.Text = "";
            }

            else if (cmbvehicletype.Text.Trim() == null || cmbvehicletype.Text.Trim() == "")
            {

                cmbvehicletype.Focus();
                lblstatus.Text = "Enter an vehicle type";
                cmbvehicletype.Text = "";
            }
            else if (txtnooftrip.Text.Trim() == null || txtnooftrip.Text.Trim() == "")
            {

                txtnooftrip.Focus();
                lblstatus.Text = "Enter No of Trip";
                txtnooftrip.Text = "";
            }

            else if (rhtfrom.Text.Trim() == null || rhtfrom.Text.Trim() == "")
            {

                rhtfrom.Focus();
                lblstatus.Text = "Confirm Source Adress";
                rhtfrom.Text = "";
            }

            else if (rhtto.Text.Trim() == null || rhtto.Text.Trim() == "")
            {

                rhtto.Focus();
                lblstatus.Text = "Confirm Destination";
                rhtto.Text = "";
            }

            else if (txtcontactperson.Text.Trim() == null || txtcontactperson.Text.Trim() == "")
            {

                txtcontactperson.Focus();
                lblstatus.Text = "Enter Contact Person";
                txtcontactperson.Text = "";
            }
     

            else
            {

                sucess = true;
            }
            return sucess;
        }
        /// <summary>
        /// when add button clicked
        /// </summary>validates the controlls
        /// 
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (validationcontrol() == true)
                {            
         jobcodedatabean = new JobCodeDataBean();

         

                jobcodedatabean.Jobcode = txtjobcode.Text;
                jobcodedatabean.PaperRecievedate1 = dtpjobcodedate.Value.Date;
                jobcodedatabean.Customercode = cmbcustomercode.Text;
                jobcodedatabean.CompanyName1 = txtcompanyname.Text;
                jobcodedatabean.Customerpk = int.Parse(lblcustpk.Text.ToString());
                jobcodedatabean.Vehicletype = cmbvehicletype.Text;

                jobcodedatabean.Totaltrip = int.Parse(txtnooftrip.Text);
                jobcodedatabean.Shipmenentdate = dtpshipmentdate.Value.Date;
                jobcodedatabean.Fromplace = rhtfrom.Text;
                jobcodedatabean.Destination = rhtto.Text;
                jobcodedatabean.Contactperson = txtcontactperson.Text;
                jobcodedatabean.Description = rhtdescription.Text;
                jobcodedatabean.Noofpackages =txtnoofpackage.Text;
                if(rhtplt.Checked==true ){
                    jobcodedatabean.Typeofpackage="Pallete";
                }
                else if(rhtloose.Checked== true)
                {
                     jobcodedatabean.Typeofpackage="Loose";
                
                }
                else if(rhtloose.Checked== true && rhtplt.Checked==true)
                {
                    
                     jobcodedatabean.Typeofpackage="Pallete/Loose";
                }


                addaction(jobcodedatabean);
               

            }

            
              
        }
        /// <summary>
        /// clearcontrols
        /// </summary>
        public void clearControl()
        {
            txtcontactperson.Text = "";
            txtjobcode.Text = "";
            txtnoofpackage.Text = "";
            txtjobcode.Enabled = true;


        }

        /// <summary>
        /// add a new jobe code to table jobcode mastertable
        /// </summary>
        /// <param name="jobcodedatabean"></param>

         public void addaction(JobCodeDataBean jobcodedatabean)
        {
            

         try
         {

                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                oleDbConnection1.Open();
                OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO jobcodemastertable (jobcode,companyName ,customercode,totaltrip,shipmentdate,fromPlace, destination,description ,packagetype ,noofpackage ,contactperson ,jobecodedate , customerpk,vehicletype ) Values ('" + jobcodedatabean.Jobcode + "', '" + jobcodedatabean.CompanyName1 + "', '" + jobcodedatabean.Customercode + "' ," + jobcodedatabean.Totaltrip + ",'" + jobcodedatabean.Shipmenentdate + "' ,'" + jobcodedatabean.Fromplace + "','" + jobcodedatabean.Destination + "','" + jobcodedatabean.Description + "','" + jobcodedatabean.Typeofpackage + "','" + jobcodedatabean.Noofpackages + "','" + jobcodedatabean.Contactperson + "','" + jobcodedatabean.PaperRecievedate1 + "'," + jobcodedatabean.Customerpk + " ,'" + jobcodedatabean.Vehicletype+ "') ", oleDbConnection1);
                oleDbCommand1.CommandType = CommandType.Text;
                oleDbCommand1.ExecuteNonQuery();

                MessageBox.Show("JOBCODE=" + jobcodedatabean.Jobcode);
                oleDbConnection1.Close();
                this.Close();


            }
         catch(Exception e) {
             MessageBox.Show("The application had met with some errors at inserting Invoice please restart  the application :\n error:" + e + "");

           }




        }
        /// <summary>
        /// to so to search customerform when the 
        /// s button is clicked
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
         private void button3_Click(object sender, EventArgs e)
         {
             //this.Close();
              CustomerSearch customersearch = new CustomerSearch();
              customersearch.ShowDialog ();
              cmbcustomercode.Text = customersearch.CustomerCode;
           
              txtcompanyname.Text = customersearch.CustomerCompany;
           
             lblcustpk.Text = customersearch.Custpk.ToString();
        
          
            
         }
        /// <summary>
        /// validates the no trip text box
        /// to recieve only numerical value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

         private void txtnooftrip_KeyPress(object sender, KeyPressEventArgs e)
         {

             // Check for the flag being set in the KeyDown event.
             if (nonNumberEntered == true)
             {
                 // Stop the character from being entered into the control since it is non-numerical.
                 e.Handled = true;
                 lblstatus.Text = "Enter A valid Number";

             }
             else
             {
                 if (e.KeyChar == '\t')
                 {
                     lblstatus.Text = "";
                     dtpshipmentdate.Focus();
                 }

             }
                
             
         }

         private void txtnooftrip_KeyDown(object sender, KeyEventArgs e)
         {
             // Initialize the flag to false.
             nonNumberEntered = false;

             // Determine whether the keystroke is a number from the top of the keyboard.
             if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
             {
                 // Determine whether the keystroke is a number from the keypad.
                 if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                 {
                     // Determine whether the keystroke is a backspace.
                     if (e.KeyCode != Keys.Back)
                     {
                         // A non-numerical keystroke was pressed.
                         // Set the flag to true and evaluate in KeyPress event.
                         nonNumberEntered = true;
                     }
                 }


             }



         }



        /// <summary>
        /// to getnew jobcode autogenerated
        /// </summary>
         public void newjobecodenoget(){
             try
             {
                 String jobcodeno = null;
                 Int32 S = 0;
                 txtjobcode.Text = "";
                 OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                 oleDbConnection1.Open();


                 String query = "SELECT TOP 1 jobpk FROM jobcodemastertable ORDER BY jobpk DESC";
                 OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);


                 DataSet ds = new DataSet();

                 dAdapter.Fill(ds, "tbljobrdataview");
                 if (ds.Tables[0].Rows.Count == 0)
                 {
                     S = 0;
                 }
                 else
                 {
                     S = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                 }

                 S++;

                 jobcodeno = "NFT" + S.ToString();

                 txtjobcode.Text = "NFT" + S.ToString();
                 txtjobcode.Enabled = false;
             }
             catch (Exception exp)
             {

                 MessageBox.Show("Failed to autogenerate jobcode ");
                 MessageBox.Show(exp.ToString());
                 this.Close();
             }
          
        }

















    }

}
